package com.ruoyi.web.controller.community.owner;

import java.util.*;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.community.domain.BaseActivityRegisterRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.OwnerActivityParticipate;
import com.ruoyi.community.service.IOwnerActivityParticipateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动签到详情Controller
 * 
 * @author ruoyi
 * @date 2021-12-13
 */
@RestController
@RequestMapping("/community/owner/participate")
public class OwnerActivityParticipateController extends BaseController
{
    @Autowired
    private IOwnerActivityParticipateService ownerActivityParticipateService;

    /**
     * 签到
     */
    @Log(title = "签到", businessType = BusinessType.INSERT)
    @PostMapping("/register")
    public AjaxResult register(@RequestBody BaseActivityRegisterRequest baseActivityRegisterRequest)
    {
        Integer result = ownerActivityParticipateService.insertOwnerActivityParticipate(baseActivityRegisterRequest);
        String errorInfo;
        if (result==-1){
            errorInfo = "您已签到，请勿重复签到！";
        }else{
            errorInfo = "签到验证码错误！";
        }
        return toAjax(result, errorInfo);
    }

    /**
     * 查询活动签到详情列表
     */
//    @PreAuthorize("@ss.hasPermi('community:participate:list')")
    @GetMapping("/list")
    public TableDataInfo list(OwnerActivityParticipate ownerActivityParticipate)
    {
        startPage();
        List<OwnerActivityParticipate> list = ownerActivityParticipateService.selectOwnerActivityParticipateList(ownerActivityParticipate);
        return getDataTable(list);
    }

    @GetMapping("/list/{activityId}")
    public AjaxResult listEnrollById(@PathVariable("activityId") String activityId)
    {
        List<SysUser> userList = ownerActivityParticipateService.selectOwnerActivityParticipateListById(activityId);
        OwnerActivityParticipate query = new OwnerActivityParticipate();
        query.setActivityId(activityId);
        List<OwnerActivityParticipate> participateList = ownerActivityParticipateService.selectOwnerActivityParticipateList(query);
        ArrayList<Object> list = new ArrayList<>();
        for (int i = 0; i < participateList.size(); i++) {
            Map enrollDict = new HashMap<>();
            enrollDict.put("record", participateList.get(i));
            enrollDict.put("userInfo", userList.get(i));
            list.add(enrollDict);
        }
        return AjaxResult.success(list);
    }

    /**
     * 导出活动签到详情列表
     */
//    @PreAuthorize("@ss.hasPermi('community:participate:export')")
    @Log(title = "活动签到详情", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OwnerActivityParticipate ownerActivityParticipate)
    {
        List<OwnerActivityParticipate> list = ownerActivityParticipateService.selectOwnerActivityParticipateList(ownerActivityParticipate);
        ExcelUtil<OwnerActivityParticipate> util = new ExcelUtil<OwnerActivityParticipate>(OwnerActivityParticipate.class);
        return util.exportExcel(list, "活动签到详情数据");
    }

    /**
     * 获取活动签到详情详细信息
     */
//    @PreAuthorize("@ss.hasPermi('community:participate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(ownerActivityParticipateService.selectOwnerActivityParticipateById(id));
    }

//    /**
//     * 新增活动签到详情
//     */
////    @PreAuthorize("@ss.hasPermi('community:participate:add')")
//    @Log(title = "活动签到详情", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody OwnerActivityParticipate ownerActivityParticipate)
//    {
//        return toAjax(ownerActivityParticipateService.insertOwnerActivityParticipate(ownerActivityParticipate));
//    }

    /**
     * 修改活动签到详情
     */
//    @PreAuthorize("@ss.hasPermi('community:participate:edit')")
    @Log(title = "活动签到详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OwnerActivityParticipate ownerActivityParticipate)
    {
        return toAjax(ownerActivityParticipateService.updateOwnerActivityParticipate(ownerActivityParticipate));
    }

    /**
     * 删除活动签到详情
     */
//    @PreAuthorize("@ss.hasPermi('community:participate:remove')")
    @Log(title = "活动签到详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(ownerActivityParticipateService.deleteOwnerActivityParticipateByIds(ids));
    }
}
